home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / xpdmenu.pro < prev   
Text File  |  1997-07-08  |  8KB  |  268 lines

  1. ; $Id: xpdmenu.pro,v 1.2 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ; Copyright (c) 1991-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;    XPDMENU
  8. ;
  9. ; PURPOSE:
  10. ;    This procedure implifies setting up widget pulldown menus. XPDMENU
  11. ;    reads a description of the menu to be generated, and calls
  12. ;    the appropriate widget creation functions to generate it.
  13. ;
  14. ; CALLING SEQUENCE:
  15. ;    XPDMENU, Desc, Parent
  16. ;
  17. ; INPUTS:
  18. ;    DESC:    Either the name of a file that contains the description of the
  19. ;        pulldown menu to be generated, or a string array that
  20. ;        contains the description.  The rules for a pull-down menu
  21. ;        description are as follows:
  22. ;
  23. ;        Leading and trailing whitespace is ignored.  Lines starting 
  24. ;        with the '#' character or blank lines are ignored.  All other 
  25. ;        lines contain 2 fields, a button label and a value.  The label
  26. ;        should be quoted with any desired delimiter, usually single 
  27. ;        or double quotes.  The value can be omitted, in which case the 
  28. ;        label is used as the value.  To make a menu choice reveal 
  29. ;        another pull-down menu, place a '{' character in its value 
  30. ;        field.  Such a pulldown is terminated by a line containing 
  31. ;        a '}' in the label field.
  32. ;        
  33. ;        Example:
  34. ;            "Colors" {
  35. ;                "Red"
  36. ;                "Green"
  37. ;                "Blue"    {
  38. ;                "Light"
  39. ;                "Medium"
  40. ;                "Dark"
  41. ;                "Navy"
  42. ;                "Royal"
  43. ;                }
  44. ;                "Cyan"
  45. ;                "Magenta"
  46. ;            }
  47. ;            "Quit"        DONE
  48. ;        
  49. ;        This example builds a menu bar with 2 buttons,
  50. ;        named "Colors" and "Quit". "Colors" is a pulldown
  51. ;        containing "Red", "Green", "Blue", "Cyan", and "Magenta".
  52. ;        "Blue" is a sub-pulldown containing shades of blue.
  53. ;        Such sub-menus can be nested to any desired level.
  54. ;        Most of the lines don't specify an explicit value. The
  55. ;        exception is "Quit", which has the value "DONE". It can
  56. ;        be instructive to run the following small program:
  57. ;        
  58. ;            a = WIDGET_BASE()
  59. ;            XPDMENU, a, 'test'    ; Test contains the above
  60. ;            widget_control, /REALIZE, a
  61. ;            uvalue=''
  62. ;            repeat begin
  63. ;              event = widget_event(a)
  64. ;              WIDGET_CONTROL, get_uvalue=uvalue, event.id
  65. ;              print, uvalue
  66. ;            end until uvalue eq "EXIT"
  67. ;            WIDGET_CONTROL, /destroy, a
  68. ;            end
  69. ;
  70. ;        Note that if you choose to make DESC be a string array,
  71. ;        the arrays contents must be exactly the same as the file
  72. ;        would be (including the quotes around the fields). Each
  73. ;        element of the array corresponds to one line of a file.
  74. ;
  75. ;    PARENT:    Widget ID of the parent base widget for the pulldown menu.  
  76. ;        If this argument is omitted, the menu base is a top-level base.
  77. ;
  78. ; KEYWORDS:
  79. ;    BASE:    A named variable to recieve the widget ID of the created base.
  80. ;
  81. ;    COLUMN:    If set, the buttons will be arranged in a column.  If unset,
  82. ;        the buttons will be arranged in a row.
  83. ;
  84. ;    FRAME:    The width, in pixels of the frame drawn around the base.  The
  85. ;        default is no frame.
  86. ;
  87. ;    TITLE:    If PARENT is not supplied, TITLE can be set a string to be
  88. ;        used as the title for the widget base.
  89. ;
  90. ;    FONT:    A string that contains the name of the font to use for the
  91. ;        menu buttons.
  92. ;
  93. ; OUTPUTS:
  94. ;    None.
  95. ;
  96. ; COMMON BLOCKS:
  97. ;    None.
  98. ;
  99. ; SIDE EFFECTS:
  100. ;    A pulldown menu widget heirarchy is created, but not realized.
  101. ;    Each button has the label specified by the first field of the
  102. ;    corresponding pulldown menu description line.  Each button has a
  103. ;    user value (uvalue) specified by the second field.
  104. ;
  105. ; RESTRICTIONS:
  106. ;    Very little syntax checking is done on the description file.
  107. ;    Incorrectly formated input can lead to unexpected results.
  108. ;
  109. ; EXAMPLE:
  110. ;    For an example of using XPDMENU, see the "Pull-Down Menu" example
  111. ;    in the "Simple Widget Examples".  To create the simple widget examples
  112. ;    main menu, enter WEXMASTER from the IDL prompt.
  113. ;
  114. ; MODIFICATION HISTORY:
  115. ;    4 October 1990, AB, RSI.
  116. ;    16 January 1991, AB    Added the option of DESC being a string 
  117. ;                array containing the description.
  118. ;-
  119.  
  120. function mkpull_getline, unit, data, idx, n, label, value
  121. ; Reads the next non-comment line from the description. If Unit is
  122. ; non-zero, it represents an open file from which the description
  123. ; is read. Otherwise, the description comes from data(idx) and idx is
  124. ; incremented. In this case, EOF is defined as idx being equal to n.
  125. ;
  126. ; LABEL is set to the label part and VALUE to the value part.
  127. ; On EOF, the file unit, if any, is closed. The return value of
  128. ; the function is:
  129. ;    -1 - End of file was seen
  130. ;    0 - Line was single button
  131. ;    1 - Line is a pulldown button
  132. ;
  133.  
  134. ret = -1
  135. value = ''
  136.  
  137. if (unit eq 0) then not_eof = (idx lt n) else not_eof = (not eof(unit))
  138. while ((not_eof) and (ret eq -1)) do begin
  139.   if (unit eq 0) then begin
  140.     value = data(idx)
  141.     idx = idx + 1
  142.   endif else begin
  143.     readf, unit, value
  144.   endelse
  145.   value = strtrim(value, 2)        ; Leading/trailing whitespace
  146.   delim = strmid(value, 0, 1)
  147.   case delim of
  148.     "" :
  149.     "#" :
  150.     "}" : ret = 2
  151.     else: begin
  152.       value = strmid(value, 1, 100000)
  153.       pos = strpos(value, delim)
  154.       if (pos eq -1) then begin
  155.     message, "Bad delimiter in line: " + delim + value, /INFORM
  156.       endif else begin
  157.         label = strmid(value, 0, pos)
  158.         value = strtrim(strmid(value, pos+1, 100000), 2)
  159.         if (value eq "{") then begin
  160.       value = "" 
  161.       ret = 1
  162.         endif else begin
  163.           if (strlen(value) eq 0) then value = label
  164.       ret = 0
  165.         endelse
  166.       endelse
  167.     end
  168.   endcase
  169. if (unit eq 0) then not_eof = (idx lt n) else not_eof = (not eof(unit))
  170. endwhile
  171.  
  172. if ((unit ne 0) and (ret eq -1)) then begin free_lun, unit & unit = 0 & end
  173.  
  174. return, ret
  175.  
  176. end
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. pro mkpull_pulldown, parent, unit, data, idx, n, font
  185. ;
  186. ; unit - A file LUN or 0.
  187. ; data - If Unit is 0, data is a string array containing the menu
  188. ;    description.
  189. ; idx - If Unit is 0, idx is an integer giving the current index into data.
  190. ; n - If Unit is 0, n is an integer giving the # of elements in data.
  191.  
  192. while 1 do begin
  193.   ret = mkpull_getline(unit, data, idx, n, label, value)
  194.  
  195.   case ret of
  196.     -1 : return
  197.     0 : begin
  198.     if font ne '' then begin
  199.         but = WIDGET_BUTTON(parent,value=label,uvalue=value,font=font)
  200.     endif else begin
  201.         but = WIDGET_BUTTON(parent, value=label, uvalue=value)
  202.     endelse
  203.     end
  204.     1 : begin
  205.         if font ne '' then begin
  206.         but = WIDGET_BUTTON(parent, value=label, MENU = 2, font=font)
  207.         endif else begin
  208.         but = WIDGET_BUTTON(parent, value=label, MENU = 2)
  209.         endelse
  210.         mkpull_pulldown, but, unit, data, idx, n, font
  211.     end
  212.     2 : return
  213.   endcase
  214. endwhile
  215.  
  216. end
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224. pro XPDMENU, DESC, PARENT, BASE=BASE, FRAME=FRAME, TITLE=TITLE,    $
  225.     COLUMN=COLUMN, FONT=FONT
  226.  
  227.   s = size(parent)
  228.   if (s(s(0) + 1) eq 0) then begin
  229.     ; No parent is specified.
  230.     parent = 0
  231.     if (not keyword_set(TITLE)) then TITLE='Menu'
  232.   endif else begin
  233.     if (s(0) ne 0) then message, 'PARENT must be a scalar value."
  234.     if (s(1) ne 3) then message, 'PARENT must be a long integer."
  235.   endelse
  236.  
  237.   s = size(desc)
  238.   if (s(s(0)+1) ne 7) then $
  239.     message,'Description argument must be of type string."
  240.   if (s(0) eq 0) then begin
  241.     openr, unit, desc, /GET_LUN
  242.     n = 0
  243.  endif else begin
  244.     if (s(0) ne 1) then message, 'String array must be 1-D."
  245.     unit = 0
  246.     n = s(1)
  247.   endelse
  248.  
  249.   if (not keyword_set(frame)) then frame = 0
  250.   if (not keyword_set(font)) then font = ''
  251.  
  252.   if (parent eq 0) then $
  253.       IF(KEYWORD_SET(COLUMN)) THEN $
  254.             base = WIDGET_BASE(/COLUMN, TITLE=TITLE, FRAME=FRAME) $
  255.       ELSE $
  256.       base = WIDGET_BASE(/ROW, TITLE=TITLE, FRAME=FRAME) $
  257.   else $
  258.       IF(KEYWORD_SET(COLUMN)) THEN $
  259.             base = WIDGET_BASE(parent, /COLUMN, FRAME=FRAME) $
  260.       ELSE $
  261.       base = WIDGET_BASE(parent, /ROW, FRAME=FRAME)
  262.  
  263.   mkpull_pulldown, base, unit, desc, 0, n, font
  264.  
  265. end
  266.  
  267.  
  268.